home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / pnmimpnoise / pnmimpnoise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  5.8 KB  |  217 lines

  1.  
  2. /************************************************************************/
  3. #define OP_NAME      "pnmimpnoise"
  4. #define VERSION      "1.02"
  5. #define DATE         "31.01.98"
  6. #define AUTHOR       "Stefan Diener"
  7. /************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <time.h>
  15. #include <sys/types.h>
  16.  
  17. #include <STIMP/pnm.c>
  18.  
  19. struct PNM_Info bild;
  20. static float Prozent=5.0;
  21. static int Amplitude=255, AmpliR=255, AmpliG=255, AmpliB=255;
  22.  
  23. void Do_It(void)
  24. {
  25.   int i, j, ende;
  26.   int x=0, y=0, bx, by;
  27.   unsigned char *dstR, *dstG, *dstB;
  28.  
  29.   const int ax=65;
  30.   const int ay=17;
  31.  
  32.   if (bild.type==TYPE_PPM)
  33.   {
  34.     dstR=bild.redDATA;
  35.     dstG=bild.greenDATA;
  36.     dstB=bild.blueDATA;
  37.   }
  38.   else dstR=bild.DATA;
  39.  
  40.   /* auf richtiges maxval achten */
  41.   if (Amplitude>bild.maxval) Amplitude=bild.maxval;
  42.   if (AmpliR>bild.maxval) AmpliR=bild.maxval;
  43.   if (AmpliG>bild.maxval) AmpliG=bild.maxval;
  44.   if (AmpliB>bild.maxval) AmpliB=bild.maxval;
  45.  
  46.   /* Zahl der zu veraendernden Punkte */
  47.   ende=(int) floor(bild.width*bild.height*Prozent/100.0);
  48.  
  49.   /* neue Folge von Zufallszahlen erzeugen */
  50.   srand((unsigned)time(NULL));
  51.  
  52.   /* Ansatz: Methode der linearen Kongruenzen */
  53.   /* Problem: ggt(bx,source.width) muesste 1 sein */
  54.   /* und ggt(by,source.height) ebenfalls 1 */
  55.   /* hier: Ausbuegeln der kleineren Periode und der */
  56.   /* ev. sichtbaren Muster durch Zufallsanteil */
  57.   bx=3*bild.width/4+1;
  58.   by=bild.height/2+1;
  59.  
  60.   /* nur Prozentsatz der Punkte veraendern */
  61.   for (j=0; j<ende; j++)
  62.   {
  63.     /* lin. Kongruenzen und Zufallsanteil */
  64.     x=(ax*x+bx+(int) floor(50.0-100.0*((double) rand()/(double)RAND_MAX)))%bild.width;
  65.     y=(ay*y+by+(int) floor(50.0-100.0*((double) rand()/(double)RAND_MAX)))%bild.height;
  66.  
  67.     /* Bildpunkt */
  68.     i=y*bild.width+x;
  69.  
  70.     /* Punkt auf vorgegebene Amplitude setzen */
  71.     if (bild.type==TYPE_PPM)
  72.     {
  73.       dstR[i]=AmpliR;
  74.       dstG[i]=AmpliG;
  75.       dstB[i]=AmpliB;
  76.     }
  77.     else dstR[i]=Amplitude;
  78.   }
  79. }
  80.  
  81. int main(int argc,char **argv)
  82. /* Hauptprogramm */
  83. {
  84.   int i;
  85.   unsigned char tempo[10];
  86.  
  87.   /* offizielle Begruessung */
  88.   PrintOpening(argc,argv);
  89.  
  90.   /* Uebergebene Parameter auswerten */
  91.   for (i=1; i<argc; i++)
  92.   {
  93.     if (argv[i][0]=='-')
  94.     {
  95.       switch (argv[i][1])
  96.       {
  97.         case 'a': strncpy(tempo, argv[i], 9);
  98.                       tempo[0]=32;
  99.                       tempo[1]=32;
  100.                       if (sscanf(tempo,"%i",&Amplitude)!=1) Amplitude=-1;
  101.                       if ((Amplitude<0) || (Amplitude>255))
  102.                       {
  103.                         PrintMessage("Wrong value for the (gray) amplitude !");
  104.                         Hilfe();
  105.                         exit(-1);
  106.                       }
  107.                       else
  108.                       {
  109.                         AmpliR=Amplitude;
  110.                         AmpliG=Amplitude;
  111.                         AmpliB=Amplitude;
  112.                       }
  113.                       break;
  114.  
  115.         case 'r': strncpy(tempo, argv[i], 9);
  116.                       tempo[0]=32;
  117.                       tempo[1]=32;
  118.                       if (sscanf(tempo,"%i",&AmpliR)!=1) AmpliR=-1;
  119.                       if ((AmpliR<0) || (AmpliR>255))
  120.                       {
  121.                         PrintMessage("Wrong value for the red amplitude !");
  122.                         Hilfe();
  123.                         exit(-1);
  124.                       }
  125.                       break;
  126.  
  127.         case 'g': strncpy(tempo, argv[i], 9);
  128.                       tempo[0]=32;
  129.                       tempo[1]=32;
  130.                       if (sscanf(tempo,"%i",&AmpliG)!=1) AmpliG=-1;
  131.                       if ((AmpliG<0) || (AmpliG>255))
  132.                       {
  133.                         PrintMessage("Wrong value for the green amplitude !");
  134.                         Hilfe();
  135.                         exit(-1);
  136.                       }
  137.                       break;
  138.  
  139.         case 'b': strncpy(tempo, argv[i], 9);
  140.                       tempo[0]=32;
  141.                       tempo[1]=32;
  142.                       if (sscanf(tempo,"%i",&AmpliB)!=1) AmpliB=-1;
  143.                       if ((AmpliB<0) || (AmpliB>255))
  144.                       {
  145.                         PrintMessage("Wrong value for the blue amplitude !");
  146.                         Hilfe();
  147.                         exit(-1);
  148.                       }
  149.                       break;
  150.  
  151.         case 'p': strncpy(tempo, argv[i], 9);
  152.                       tempo[0]=32;
  153.                       tempo[1]=32;
  154.                       if (sscanf(tempo,"%f",&Prozent)!=1) Prozent=0.0;
  155.                       if ((Prozent<=0.0) || (Prozent>100.0))
  156.                       {
  157.                         PrintMessage("Wrong value for the percentage !");
  158.                         Hilfe();
  159.                         exit(-1);
  160.                       }
  161.                       break;
  162.  
  163.         case 'v': beVerbose=FALSE;
  164.                       break;
  165.  
  166.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  167.                      Hilfe();
  168.                      exit(-1);
  169.                      break;
  170.       }
  171.     }
  172.  
  173.     if (argv[i][0]=='+')
  174.     {
  175.       switch (argv[i][1])
  176.       {
  177.         case 'v': beVerbose=TRUE;
  178.                       break;
  179.  
  180.         default: PrintMessage("Unknown parameter: %s", argv[i]);
  181.                      Hilfe();
  182.                      exit(-1);
  183.                      break;
  184.       }
  185.     }
  186.   }
  187.  
  188.   /* Mindestzahl der Argumente pruefen */
  189.   if (argc<3)
  190.   {
  191.     PrintMessage("Wrong number of arguments !");
  192.     Hilfe();
  193.     exit(-1);
  194.   }
  195.  
  196.   /* Anzahl der Dateinamen überprüfen */
  197.   if (FilenameCount(argc, argv)!=2)
  198.   {
  199.     PrintMessage("Wrong number of file names !");
  200.     Hilfe();
  201.     exit(-1);
  202.   }
  203.  
  204.   if (ReadPNMFile(GetFilename(1,argc,argv), TYPE_PNM, &bild)==0)
  205.   {
  206.     PrintMessage("Working ...");
  207.     Do_It();
  208.  
  209.     WritePNMFile(GetFilename(2,argc,argv), &bild);
  210.     FreePNMArray(&bild);
  211.   }
  212.  
  213.   PrintClosing();
  214.   exit(0);
  215. }
  216.  
  217.